package com.cole.miniapp.project.service.impl;

import com.cole.miniapp.common.exception.AuthorizationExtendException;
import com.cole.miniapp.common.service.RedisHashService;
import com.cole.miniapp.project.domain.FcmBusiness;
import com.cole.miniapp.project.domain.FcmBusinessKey;
import com.cole.miniapp.project.mapper.FcmBusinessMapper;
import com.cole.miniapp.project.service.StoreAuthService;
import com.cole.miniapp.project.vo.SessionInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: Ceanie
 * @Despriction:
 * @Date: Created in 14:09 2019-05-20
 * @Modify By:
 **/
@Service
public class StoreAuthServiceImpl implements StoreAuthService {

    @Autowired
    @Qualifier("redisSessionServiceByHash")
    RedisHashService<String, SessionInfoEntity> sessionService;

    @Autowired
    private FcmBusinessMapper fcmBusinessMapper;

    @Override
    public FcmBusiness selectBusinessByKey(String token,Long userId , Long storeId) {
        SessionInfoEntity sessionInfoEntity = sessionService.get(userId.toString());

        Date current=new Date();
        long time=current.getTime()-sessionInfoEntity.getCreatetime().getTime();
        int outTime=2*60*60*1000;
        if (time>outTime){
            throw new AuthorizationExtendException();
        }
        if (!token.equals(sessionInfoEntity.getLoginToken())) {
            throw new AuthorizationExtendException();
        }

        FcmBusinessKey fcmBusinessKey=new FcmBusinessKey();
        fcmBusinessKey.setUserId(userId);
        fcmBusinessKey.setStoreId(storeId);

        return fcmBusinessMapper.selectByPrimaryKey(fcmBusinessKey);

    }
}
